<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
 <head>
  <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  <title>Execute an external program</title>

 </head>
 <body><div class="manualnavbar" style="text-align: center;">
 <div class="prev" style="text-align: left; float: left;"><a href="function.escapeshellcmd.html">escapeshellcmd</a></div>
 <div class="next" style="text-align: right; float: right;"><a href="function.passthru.html">passthru</a></div>
 <div class="up"><a href="ref.exec.html">Program execution 函数</a></div>
 <div class="home"><a href="index.html">PHP Manual</a></div>
</div><hr /><div id="function.exec" class="refentry">
 <div class="refnamediv">
  <h1 class="refname">exec</h1>
  <p class="verinfo">(PHP 4, PHP 5)</p><p class="refpurpose"><span class="refname">exec</span> &mdash; <span class="dc-title">Execute an external program</span></p>

 </div>

 <div class="refsect1 description" id="refsect1-function.exec-description">
  <h3 class="title">说明</h3>
  <div class="methodsynopsis dc-description">
   <span class="type">string</span> <span class="methodname"><strong>exec</strong></span>
    ( <span class="methodparam"><span class="type">string</span> <code class="parameter">$command</code></span>
   [, <span class="methodparam"><span class="type">array</span> <code class="parameter reference">&$output</code></span>
   [, <span class="methodparam"><span class="type">int</span> <code class="parameter reference">&$return_var</code></span>
  ]] )</div>

  <p class="para rdfs-comment">
   <span class="function"><strong>exec()</strong></span> executes the given
   <em><code class="parameter">command</code></em>.
  </p>
 </div>


 <div class="refsect1 parameters" id="refsect1-function.exec-parameters">
  <h3 class="title">参数</h3>
  <p class="para">
   <dl>

    
     <dt>
<em><code class="parameter">command</code></em></dt>

     <dd>

      <p class="para">
       The command that will be executed.
      </p>
     </dd>

    
    
     <dt>
<em><code class="parameter">output</code></em></dt>

     <dd>

      <p class="para">
       If the <em><code class="parameter">output</code></em> argument is present, then the
       specified array will be filled with every line of output from the
       command.  Trailing whitespace, such as <em>\n</em>, is not
       included in this array.  Note that if the array already contains some
       elements, <span class="function"><strong>exec()</strong></span> will append to the end of the array.
       If you do not want the function to append elements, call
       <span class="function"><a href="function.unset.html" class="function">unset()</a></span> on the array before passing it to
       <span class="function"><strong>exec()</strong></span>.
      </p>
     </dd>

    
    
     <dt>
<em><code class="parameter">return_var</code></em></dt>

     <dd>

      <p class="para">
       If the <em><code class="parameter">return_var</code></em> argument is present
       along with the <em><code class="parameter">output</code></em> argument, then the
       return status of the executed command will be written to this
       variable.
      </p>
     </dd>

    
   </dl>

  </p>
 </div>


 <div class="refsect1 returnvalues" id="refsect1-function.exec-returnvalues">
  <h3 class="title">返回值</h3>
  <p class="para">
   The last line from the result of the command.  If you need to execute a 
   command and have all the data from the command passed directly back without 
   any interference, use the <span class="function"><a href="function.passthru.html" class="function">passthru()</a></span> function.
  </p>
  <p class="para">
   To get the output of the executed command, be sure to set and use the
   <em><code class="parameter">output</code></em> parameter.
  </p>
 </div>


 <div class="refsect1 examples" id="refsect1-function.exec-examples">
  <h3 class="title">范例</h3>
  <p class="para">
   <div class="example" id="example-3583">
    <p><strong>Example #1 An <span class="function"><strong>exec()</strong></span> example</strong></p>
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br /></span><span style="color: #FF8000">//&nbsp;outputs&nbsp;the&nbsp;username&nbsp;that&nbsp;owns&nbsp;the&nbsp;running&nbsp;php/httpd&nbsp;process<br />//&nbsp;(on&nbsp;a&nbsp;system&nbsp;with&nbsp;the&nbsp;"whoami"&nbsp;executable&nbsp;in&nbsp;the&nbsp;path)<br /></span><span style="color: #007700">echo&nbsp;</span><span style="color: #0000BB">exec</span><span style="color: #007700">(</span><span style="color: #DD0000">'whoami'</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
    </div>

   </div>
  </p>
 </div>


 <div class="refsect1 notes" id="refsect1-function.exec-notes">
  <h3 class="title">注释</h3>
  <div class="warning"><strong class="warning">Warning</strong><p class="para">当用户提供的数据传入此函数，使用
<span class="function"><a href="function.escapeshellarg.html" class="function">escapeshellarg()</a></span> 或 <span class="function"><a href="function.escapeshellcmd.html" class="function">escapeshellcmd()</a></span>
来确保用户欺骗系统从而执行任意命令。</p></div>
  <blockquote class="note"><p><strong class="note">Note</strong>: <p class="para">如何程序使用此函数启动，为了能保持在后台运行，此程序必须将输出重定向到文件或其它输出流。否则会导致
PHP 挂起，直至程序执行结束。</p></p></blockquote>
  <blockquote class="note"><p><strong class="note">Note</strong>: <span class="simpara"><a href="features.safe-mode.html" class="link">安全模式</a> 启用时，可仅可用
<a href="ini.sect.safe-mode.html#ini.safe-mode-exec-dir" class="link">safe_mode_exec_dir</a> 执行文件。实际上，现在不允许在到可执行的路径中存在 <em>..</em> 组件。</span></p></blockquote>
  <div class="warning"><strong class="warning">Warning</strong><p class="simpara"><a href="features.safe-mode.html" class="link">安全模式</a> 启用时，命令字符串会被
<span class="function"><a href="function.escapeshellcmd.html" class="function">escapeshellcmd()</a></span> 转换。因此，<em>echo y | echo x</em> 会变成
<em>echo y \| echo x</em>。</p></div>
 </div>


 <div class="refsect1 seealso" id="refsect1-function.exec-seealso">
  <h3 class="title">参见</h3>
  <p class="para">
   <ul class="simplelist">
    <li class="member"><span class="function"><a href="function.system.html" class="function" rel="rdfs-seeAlso">system()</a> - Execute an external program and display the output</span></li>
    <li class="member"><span class="function"><a href="function.passthru.html" class="function" rel="rdfs-seeAlso">passthru()</a> - Execute an external program and display raw output</span></li>
    <li class="member"><span class="function"><a href="function.escapeshellcmd.html" class="function" rel="rdfs-seeAlso">escapeshellcmd()</a> - Escape shell metacharacters</span></li>
    <li class="member"><span class="function"><a href="function.pcntl-exec.html" class="function" rel="rdfs-seeAlso">pcntl_exec()</a> - 在当前进程空间执行指定程序</span></li>
    <li class="member"><a href="language.operators.execution.html" class="link">backtick operator</a></li>
   </ul>
  </p>
 </div>

</div><hr /><div class="manualnavbar" style="text-align: center;">
 <div class="prev" style="text-align: left; float: left;"><a href="function.escapeshellcmd.html">escapeshellcmd</a></div>
 <div class="next" style="text-align: right; float: right;"><a href="function.passthru.html">passthru</a></div>
 <div class="up"><a href="ref.exec.html">Program execution 函数</a></div>
 <div class="home"><a href="index.html">PHP Manual</a></div>
</div></body></html>
